//
//  DetailOrderInfoViewController.m
//  智积分
//
//  Created by apple on 2018/5/15.
//  Copyright © 2018年 Cynic. All rights reserved.
//

#import "DetailCodeStoreOrderViewController.h"
#import "OrderUserinfoTableViewCell.h"
#import "OrderProductioninfoTableViewCell.h"
#import "OrderCreateinfoTableViewCell.h"
#import "OrderMessageTableViewCell.h"
static NSString *OrderUserinfoTableViewCellindentifier = @"OrderUserinfoTableViewCellindentifier";
static NSString *OrderProductioninfoTableViewCellindentifier = @"OrderProductioninfoTableViewCellindentifier";
static NSString *OrderCreateinfoTableViewCellindentifier = @"OrderCreateinfoTableViewCellindentifier";
static NSString *OrderMessageTableViewCellindentifier = @"OrderMessageTableViewCellindentifier";
@interface DetailCodeStoreOrderViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,retain)UITableView *MainView;
@property (nonatomic ,retain)NSMutableDictionary *alldata;
@end

@implementation DetailCodeStoreOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self viewLoad];
}

//-(instancetype)initWithData:(NSDictionary *)data{
//    self = [super init];
//    _alldata = [data mutableCopy];
//    _data = [NSMutableArray arrayWithArray:data[@"order_lst"]];
//    return self;
//}

-(void)loadDatawithCode:(NSString *)code{
    NetWorkModel *netmodel = [[NetWorkModel alloc] init];
    [netmodel GetDetailCodeStoreInfoByShopID:[NSString stringWithFormat:@"%@",code]];
    WEAKSELF;
    netmodel.sendMessage = ^(NSString *result) {
        if ([result isEqual:@"success"]) {
            _alldata = [cxsingler.DicData mutableCopy];
            [weakSelf.MainView reloadData];
        }
        else{
            
        }
    };
}

-(void)viewLoad{
    [self setNavigationViewWithTitle:@"订单详情" WithCanPop:YES WithPopData:@"black_return" WithPopisString:NO WithHasRightOperation:NO WithrightOperationData:@"" WithRightisString:NO WithNavigationColor:[UIColor whiteColor] withBgView:self.view];
    UIView *navigationlayerview =[[ UIView alloc] initWithFrame:CGRectMake(0, 44 + cxsingler.topdis - 1, cx_wid, 1)];
    navigationlayerview.backgroundColor = [CXSinglerModel colorWithHex:@"E2E2E2" alpha:1];
    [self.view addSubview:navigationlayerview];
    self.centerTitle.textColor = [UIColor blackColor];
    [self MainViewLoad];
}

-(void)MainViewLoad{
    _MainView = [[UITableView alloc] initWithFrame:CGRectMake(0, cxsingler.topdis + 44, cx_wid, cx_hit - cxsingler.topdis - 44 - cxsingler.bottomdis)];
    _MainView.delegate = self;
    _MainView.backgroundColor = [CXSinglerModel colorWithHex:@"f3f4f6" alpha:1];
    _MainView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_MainView registerClass:[OrderUserinfoTableViewCell class] forCellReuseIdentifier:OrderUserinfoTableViewCellindentifier];
    [_MainView registerClass:[OrderProductioninfoTableViewCell class] forCellReuseIdentifier:OrderProductioninfoTableViewCellindentifier];
    [_MainView registerClass:[OrderCreateinfoTableViewCell class] forCellReuseIdentifier:OrderCreateinfoTableViewCellindentifier];
    [_MainView registerClass:[OrderMessageTableViewCell class] forCellReuseIdentifier:OrderMessageTableViewCellindentifier];
    _MainView.dataSource = self;
    [self.view addSubview:_MainView];
}

#pragma mark--uitableview代理开始
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 1) {
        return  1;
    }
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row = indexPath.row;
    NSInteger sec = indexPath.section;
    if (sec == 0) {
        if ([[self Nnll:_alldata[@"logisticsNo"] withPlace:@""] length] == 0 || [_alldata[@"logisticsNo"] isKindOfClass:[NSNull class]]) {
            return 145;
        }
        else{
            return 145 + 64;
        }
    }
    else if (sec == 1){
        //        part[@"goods_attr"]
        CGFloat height = 16;
        NSString *destext = @"testdes";
        UIFont *fnt = [UIFont systemFontOfSize:13];
        height = ceil([destext boundingRectWithSize:CGSizeMake(cx_wid - 112, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil] context:nil].size.height);
        if (height < 16) {
            height = 16;
        }
        CGFloat result = 124 - 16 + height;
        return 124 - 16 + height;
    }
    else if (sec == 2){
        NSString *notes = @"";
        CGFloat height = 16;
        //        for (NSDictionary *dic in _data) {
        //            NSString *partnote = dic[@"order_notes"];
        //            if (partnote.length > 0) {
        //                if (notes.length > 0) {
        //                    partnote = [@"," stringByAppendingString:partnote];
        //                }
        //                notes = [notes stringByAppendingString:partnote];
        //            }
        //
        //        }
        notes = @"testnote";
        UIFont *fnt = [UIFont systemFontOfSize:14];
        height = ceil([notes boundingRectWithSize:CGSizeMake(cx_wid - 78 - 12, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil] context:nil].size.height);
        if (height < 16) {
            height = 16;
        }
        return 130 - 16 + height;
    }
    else if (sec == 3){
        return 130;
    }
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger sec = indexPath.section;
    NSInteger row = indexPath.row;
    if (sec == 0) {
        OrderUserinfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:OrderUserinfoTableViewCellindentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.status.text = [self statusByWord:_alldata[@"tradeStatus"]];
        cell.name.text = _alldata[@"userName"];
        cell.phone.text = _alldata[@"mobile"];
        cell.place.text = _alldata[@"address"];
        if ([[self Nnll:_alldata[@"logisticsNo"] withPlace:@""] length] == 0 || [_alldata[@"logisticsNo"] isKindOfClass:[NSNull class]]) {
            cell.transid.hidden = YES;
            cell.transimg.hidden = YES;
            cell.transname.hidden = YES;
            cell.bottomlayerview.hidden = YES;
        }
        else{
            cell.transid.hidden = NO;
            cell.transimg.hidden = NO;
            cell.transname.hidden = NO;
            cell.bottomlayerview.hidden = NO;
            cell.transname.text = _alldata[@"logisticsName"];
            cell.transid.text = [NSString stringWithFormat:@"快递单号:%@",_alldata[@"logisticsNo"]];
        }
        return cell;
    }
    else if (sec == 1){
        NSInteger row = indexPath.row;
        OrderProductioninfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:OrderProductioninfoTableViewCellindentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.name.text = _alldata[@"itemTitle"];
        cell.price.text = [NSString stringWithFormat:@"%.2lf",[_alldata[@"price"] floatValue] / 100];
        cell.num.text = [NSString stringWithFormat:@"x%@",_alldata[@"num"]];
        UIFont *fnt = [UIFont systemFontOfSize:13];
        CGFloat nameheight = 18;
        nameheight = ceil([[self Nnll:_alldata[@"goods_attr"] withPlace:@""] boundingRectWithSize:CGSizeMake(cx_wid - 120, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil] context:nil].size.height);
        if ( nameheight < 18) {
            nameheight = 18;
        }
        cell.des.text = [self Nnll:_alldata[@"goods_attr"] withPlace:@""];
        cell.des.bounds = CGRectMake(0, 0, cx_wid - 120, nameheight);
        CGFloat height = 16;
        NSString *destext = _alldata[@"good_attr"];
        height = ceil([destext boundingRectWithSize:CGSizeMake(cx_wid - 112, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil] context:nil].size.height);
        if (height < 16) {
            height = 16;
        }
        cell.des.bounds = CGRectMake(0, 0, cx_wid - 112, height);
        NSString *imgurl = [self Nnll:_alldata[@"url"] withPlace:@""];
        if (![imgurl containsString:@"http"]) {
            imgurl = [@"http:" stringByAppendingString:imgurl];
        }
        [cell.img sd_setImageWithURL:[NSURL URLWithString:imgurl] placeholderImage:[UIImage imageNamed:@"logo"]];
        //        NSLog(@"%@",)
        return cell;
    }
    else if (sec == 2){
        OrderMessageTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:OrderMessageTableViewCellindentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSString *notes = @"";
#pragma mark--无重复
        //        for (NSDictionary *dic in _data) {
        //            NSString *partnote = dic[@"order_notes"];
        //            if (partnote.length > 0) {
        //                if (notes.length > 0) {
        //                    partnote = [@"," stringByAppendingString:partnote];
        //                }
        //                notes = [notes stringByAppendingString:partnote];
        //            }
        //        }
        notes = _alldata[@"order_notes"];
        cell.usernote.text = notes;
        UIFont *fnt = [UIFont systemFontOfSize:14];
        CGFloat height = ceil([notes boundingRectWithSize:CGSizeMake(cx_wid - 78 - 12, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil] context:nil].size.height);
        if (height < 16) {
            height = 16;
        }
        cell.usernote.bounds = CGRectMake(0, 0, cx_wid - 78 - 12, height);
        cell.freeMoney.text = @"";
        NSString *transmoney = [NSString stringWithFormat:@"%.2lf",[_alldata[@"freight"] floatValue] / 100 ];
        if ([transmoney floatValue] > 0) {
            cell.transMoney.text = transmoney;
        }
        else{
            cell.transMoney.text = @"包邮";
        }
         cell.costMoney.text = [NSString stringWithFormat:@"￥%.2lf + %ld 积分",[_alldata[@"payAmount"] floatValue] / 100, [_alldata[@"payIntegral"] integerValue]];
//        cell.costMoney.text = [NSString stringWithFormat:@"%.2lf",[_alldata[@"totalAmount"] floatValue] / 100];
        return cell;
    }
    else if (sec == 3){
        OrderCreateinfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:OrderCreateinfoTableViewCellindentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.orderid.text = _alldata[@"tradeNo"];
        cell.createtime.text = [NSString stringWithFormat:@"%@",[self gettimeByInter:[_alldata[@"createDate"] integerValue]]];
        cell.starttime.text = [NSString stringWithFormat:@"%@",[self gettimeByInter:[_alldata[@"payDate"] integerValue]]];
        return cell;
    }
    return nil;
}
#pragma mark--uitableview代理结束

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

-(NSString *)statusByWord:(NSString *)statusWord{
    NSString *returnStr = @"未知";
    if ([statusWord isEqual:@"FINISH"]) {
        returnStr = @"交易完成";
    }
    else if ([statusWord isEqual:@"PAYING"]){
        returnStr = @"待支付";
    }
    else if ([statusWord isEqual:@"WAIT_SHIPPING"]){
        returnStr = @"待发货";
    }
    else if ([statusWord isEqual:@"SHIPPING"]){
        returnStr = @"待收货";
    }
    return returnStr;
}
@end

